package com.jilian.mydemo.modul.main.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.MsgDto;
import com.jilian.mydemo.common.dto.MsgListDto;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.main.adapter.MsgAdapter;
import com.jilian.mydemo.modul.main.viewmodel.MainViewModel;
import com.jilian.mydemo.modul.msg.ui.MsgDatailActivity;
import com.jilian.mydemo.utils.EmptyUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * 消息列表
 */
public class ThreeFragment extends BaseFragment implements CustomItemClickListener {
    private TextView tvOne;
    private TextView tvTwo;
    private RelativeLayout rlOne;
    private RelativeLayout rlTwo;
    private RecyclerView recyclerview;
    private MsgAdapter msgAdapter;
    private LinearLayoutManager linearLayoutManager;
    private List<MsgDto> datas;
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;
    private Integer pageNum = 1;//
    private Integer pageSize = 20;//
    private int type = 2;//1 系统消息 2 通知消息
    private MainViewModel viewModel;
    @Override
    protected void loadData() {

    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_three;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        setNormalTitle(view, "消息", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_left_text);
        imageView.setVisibility(View.GONE);
        tvOne = (TextView) view.findViewById(R.id.tv_one);
        tvTwo = (TextView) view.findViewById(R.id.tv_two);
        rlOne = (RelativeLayout) view.findViewById(R.id.rl_one);
        rlTwo = (RelativeLayout) view.findViewById(R.id.rl_two);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        msgAdapter = new MsgAdapter(getActivity(), datas, this);
        recyclerview.setAdapter(msgAdapter);
        srHasData = (SmartRefreshLayout) view.findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) view.findViewById(R.id.sr_no_data);
        srNoData.setEnableLoadMore(false);

    }

    @Override
    protected void initData() {
        getMsgList();
    }

    /**
     * 获取消息列表
     */
    private void getMsgList() {
        viewModel.getMsgList(pageNum, pageSize, type);
        viewModel.getMsgliveData().observe(this, new Observer<BaseDto<MsgListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<MsgListDto> listBaseDto) {
                hideLoadingDialog();
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                //有数据
                if (listBaseDto.isSuccess()&&EmptyUtils.isNotEmpty(listBaseDto.getData().getList())) {
                    srNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        datas.clear();
                    }
                    datas.addAll(listBaseDto.getData().getList());
                    msgAdapter.notifyDataSetChanged();
                } else {
                    //说明是上拉加载
                    if (pageNum > 1) {
                        pageNum--;
                    } else {
                        //第一次就没数据
                        srNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

        @Override
        protected void initListener() {


            RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg eventMsg) throws Exception {
                if (eventMsg != null&&eventMsg.getMsgRefreshDto()!=null) {
                    if (eventMsg.getMsgRefreshDto().getCode() == 100) {
                        //showLoadingDialog();
                        getMsgList();
                    }
                }
            }
        });
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                getMsgList();
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                getMsgList();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                getMsgList();
            }
        });
        tvOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvOne.setTextColor(getResources().getColor(R.color.color_main_title));
                tvTwo.setTextColor(getResources().getColor(R.color.color_hint));
                rlOne.setVisibility(View.VISIBLE);
                rlTwo.setVisibility(View.INVISIBLE);
                pageNum = 1;
                type = 2;
                showLoadingDialog();
                getMsgList();
            }
        });
        tvTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tvOne.setTextColor(getResources().getColor(R.color.color_hint));
                tvTwo.setTextColor(getResources().getColor(R.color.color_main_title));
                rlOne.setVisibility(View.INVISIBLE);
                rlTwo.setVisibility(View.VISIBLE);
                pageNum = 1;
                type = 1;
                showLoadingDialog();
                getMsgList();
            }
        });

    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(getActivity(), MsgDatailActivity.class);
        intent.putExtra("data",datas.get(position));
        startActivity(intent);
    }
}
